package com.tongtailian.www.stir.bean;

import com.tongtailian.www.stir.utils.LogUtil;

/**
 * @author liuml
 * @explain
 * @time 2020/10/11 12:53
 */
public class DeviceStateFram extends BaseDataFram {

    /**
     * Byte[0]: 当前应用名称，Name
     * 0x01 研磨工作应用
     * 0x02 定位应用
     * 0x03 清洗应用
     * Byte[1]: 当前流程，Flow
     * 0x01 位置初始化
     * 0x02 定位流程
     * 0x03 超声波清洗
     * 0x04 热风烘干
     * 0x05 清水清洗
     * 0x06 研磨工作
     */
    private int name;
    private int Flow;
//    Byte[2]: 当前步骤，Step
//    Byte[3]: 当前数值，Data
    private int Step;
    private int DataDev;
    private int wishNum;
    private int loopNum;

    public int getWishNum() {
        return wishNum;
    }

    public void setWishNum(int wishNum) {
        this.wishNum = wishNum;
    }

    public int getLoopNum() {
        return loopNum;
    }

    public void setLoopNum(int loopNum) {
        this.loopNum = loopNum;
    }

    public int getStep() {
        return Step;
    }

    public void setStep(int step) {
        Step = step;
    }

    public int getDataDev() {
        return DataDev;
    }

    public void setDataDev(int dataDev) {
        DataDev = dataDev;
    }

    public DeviceStateFram(String param, String data) {
        this.type = BaseDataFramType.devicesState;
        this.param = param;
        this.data = data;
//        super("70", param, data);
    }


    @Override
    public void parseFrame(String frame) {
        super.parseFrame(frame);
        int position = 0;
        //具体解析
        LogUtil.d("解析数据:" + data);
        name = parseIntResult(position, data2);

        position = position + data2;
        Flow = parseIntResult(position, data2);

        position = position + data2;
        Step = parseIntResult(position, data2);

        position = position + data2;
        DataDev = parseIntResult(position, data2);

        position = position + data2;
        wishNum = parseIntResult(position, data2);

        position = position + data2;
        loopNum = parseIntResult(position, data2);
    }

    public int getName() {
        return name;
    }

    public void setName(int name) {
        this.name = name;
    }

    public int getFlow() {
        return Flow;
    }

    public void setFlow(int flow) {
        Flow = flow;
    }

    public static String decode(String encoded) {
//        byte a0 = (byte) Integer.parseInt(encoded.substring(0, 2), 16);
//        byte a1 = (byte) Integer.parseInt(encoded.substring(2), 16);
//        byte[] gbk = new byte[]{a0, a1};
//        String chinese = null;
//        try {
//            chinese = new String(gbk, "GBK");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        return chinese;
        return "";
    }

    public static String encode(String str) {
        //根据默认编码获取字节数组
        byte[] bytes = str.getBytes();
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        //将字节数组中每个字节拆解成2位16进制整数
        for (int i = 0; i < bytes.length; i++) {
            sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
            sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
        }
        return sb.toString();
    }

    private static String hexString = "0123456789ABCDEF";

    /**
     * 十六进制转换字符串
     *
     * @return String 对应的字符串
     */
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;

        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }


    public String bytes2String(byte[] data) {
        String getString = "";
        for (int i = 0; i < data.length; i++) {
            getString += String.format("%02X", data[i]);

        }
        return getString;

    }

    @Override
    public String
    toString() {
        return "DeviceStateFram{" +
                "name=" + name +
                ", Flow=" + Flow +
                ", Step=" + Step +
                ", DataDev=" + DataDev +
                ", wishNum=" + wishNum +
                ", loopNum=" + loopNum +
                '}';
    }
}
